package main

import "fmt"

// 收入接口
type Income interface {
	calculate() int // 返回项目收入
	source() string // 返回项目名称
}

// FixedBilling项目
type FixedBilling struct {
	projectName  string // 项目名称
	biddedAmount int    // 组织投标金额
}

func (fb FixedBilling) calculate() int {
	return fb.biddedAmount
}

func (fb FixedBilling) source() string {
	return fb.projectName
}

// TimeAndMaterial项目
type TimeAndMaterial struct {
	projectName string
	noOfHours   int // 时长
	hourlyRate  int // 每小时收费
}

func (tm TimeAndMaterial) calculate() int {
	return tm.noOfHours * tm.hourlyRate
}

func (tm TimeAndMaterial) source() string {
	return tm.projectName
}

// 新增收益流，广告业务
type Advertisement struct {
	adName     string
	CPC        int
	noOfClicks int
}

func (a Advertisement) calculate() int {
	return a.CPC * a.noOfClicks
}

func (a Advertisement) source() string {
	return a.adName
}

// 计算总收入
func calculateNetIncome(ic []Income) {
	var newincome int = 0
	for _, income := range ic {
		fmt.Printf("Income From %s = $%d\n", income.source(), income.calculate())
		newincome += income.calculate()
	}
	fmt.Println("总收入为 ", newincome)
}

func test() {
	fmt.Println("多态实践")
	fmt.Println("计算不同项目收益，及总和")
	p1 := FixedBilling{
		projectName:  "p1",
		biddedAmount: 100,
	}
	p2 := TimeAndMaterial{
		projectName: "p2",
		noOfHours:   10,
		hourlyRate:  200,
	}

	p3 := Advertisement{
		adName:     "p3",
		CPC:        10,
		noOfClicks: 20,
	}
	incomeStreams := []Income{p1, p2, p3}
	calculateNetIncome(incomeStreams)

}

func main() {
	test()
}
